package com.zjsd.sqeybusiness.utils;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.zjsd.sqeybusiness.domain.Sjinfo;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WeChatTemplateMessageUtil {
    private static final String TEMPLATE_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
    /**
     * 发送模板消息
     *
     * @param accessToken 微信公众号的access_token
     * @param openId      接收者openid
     * @param templateId  模板ID
     * @param url         点击模板消息后跳转的链接
     * @param data        模板消息的数据
     * @return 发送结果
     */
    public static String sendTemplateMessage(String accessToken, String openId, String templateId, String url, JSONObject data) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(TEMPLATE_MESSAGE_URL + accessToken);
        JSONObject requestBody = new JSONObject();
        requestBody.put("touser", openId);
        requestBody.put("template_id", templateId);
        requestBody.put("url", url);
        requestBody.put("data", data);
        httpPost.setEntity(new StringEntity(requestBody.toJSONString(), "UTF-8"));
        httpPost.setHeader("Content-type", "application/json");
        System.out.println("调用推送" + requestBody.toJSONString());
        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                System.out.println("成功推送" + requestBody.toJSONString());
                return EntityUtils.toString(entity, "UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        // 示例调用
        String accessToken = "90_fp583iUk-5Mjpb-kaCaex3QzERnQq_3NPDNcEfCxs1XGX0D6kfxi6zJcGWxGLzD7g1lsrshPc9i_mMocHxwRgOr2V9EP9Q0k4CAePKpMhMisMhjVmJc2wFJiIhYGPMbAAAWFC";
        String openId = "ouJl66psaliGUDZek8uumARWqkJQ";
        String templateId = "qaYTfG0uDG9E8EufutgQkX38_qNwqMUGlXA9kRdb6i8";
        String url = "https://sqeybusiness.abcsale.cn/sjinfo?sjid=" + "4"; //跳转地址
        JSONObject data = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(templateId.equals(Constants.KHYYCLTZ_TEMPLATE_ID)){
            //姓名
            data.put("thing1", new JSONObject().fluentPut("value", "王先生").fluentPut("color", "#173177"));
            //手机号
            data.put("phone_number2", new JSONObject().fluentPut("value", "18392839283").fluentPut("color", "#173177"));
            //项目名称
            data.put("thing5", new JSONObject().fluentPut("value", "【测试】5层自建房采购一台承载3人的电梯\n").fluentPut("color", "#173177"));
            //预约时间
            data.put("time4", new JSONObject().fluentPut("value", sdf.format(new Date())).fluentPut("color", "#173177"));
        }
        String result = sendTemplateMessage(accessToken, openId, templateId, url, data);
        System.out.println(result);

       /* Sjinfo sjinfo = new Sjinfo();
        sjinfo.setCgrphone("13800138000");
        String maskedNumber = sjinfo.getCgrphone().replaceAll("(\\d{3})\\d{8}(\\d{0})", "$1********$2");
        System.out.println("Masked Phone: " + maskedNumber);*/
    }


    public static String maskPhoneNumber(String phoneNumber) {
        if (phoneNumber == null || phoneNumber.length() != 11) {
            return phoneNumber; // 如果手机号为空或长度不为11位，直接返回
        }
        return phoneNumber.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

}

